package com.ruoyi.hbs.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.hbs.domain.MsfxBusinessStatistics;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.hbs.mapper.MsfxBusinessStatisticsMapper;
import com.ruoyi.hbs.service.MsfxBusinessStatisticsService;
@Service
public class MsfxBusinessStatisticsServiceImpl extends ServiceImpl<MsfxBusinessStatisticsMapper, MsfxBusinessStatistics> implements MsfxBusinessStatisticsService {


    @Autowired
    private MsfxBusinessStatisticsMapper msfxBusinessStatisticsMapper;
    /**
     * 获取按商业统计的报表数据
     * @return List<MsfxBusinessStatistics> 统计结果列表
     */
    @Override
    public List<MsfxBusinessStatistics> syncBusinessStatisticsReport() {
        // 获取按商业出库统计的报表数据
        List<MsfxBusinessStatistics> outBusinessStatisticsReport = msfxBusinessStatisticsMapper.getBusinessStatisticsOutReport();
        // 获取按商业入库统计的报表数据
        List<MsfxBusinessStatistics> inBusinessStatisticsReport = msfxBusinessStatisticsMapper.getBusinessStatisticsinReport();
        if (CollectionUtil.isNotEmpty(outBusinessStatisticsReport)) {
            for (MsfxBusinessStatistics outStatistics : outBusinessStatisticsReport) {
                if (CollectionUtil.isNotEmpty(inBusinessStatisticsReport)) {
                    for (MsfxBusinessStatistics inStatistics : inBusinessStatisticsReport) {
                        if (outStatistics.getProductId().equals(inStatistics.getProductId()) && outStatistics.getBusinessId().equals(inStatistics.getBusinessId())) {
                            outStatistics.setInboundQuantity(inStatistics.getInboundQuantity());
                        }
                    }
                }
            }
        }

        if (CollectionUtil.isNotEmpty(outBusinessStatisticsReport)) {
            // 根据productId和businessId判断是更新还是新增
            for (MsfxBusinessStatistics statistics : outBusinessStatisticsReport) {
                LambdaQueryWrapper<MsfxBusinessStatistics> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(MsfxBusinessStatistics::getProductId, statistics.getProductId())
                        .eq(MsfxBusinessStatistics::getBusinessId, statistics.getBusinessId());

                if (this.count(queryWrapper) > 0) {
                    // 存在则更新
                    this.update(statistics, queryWrapper);
                } else {
                    // 不存在则插入
                    this.save(statistics);
                }
            }
        }
        return outBusinessStatisticsReport;
    }


    /**
     * 按商业统计报表数据
     * @return List<MsfxBusinessStatistics> 统计结果列表
     */
    @Override
    public List<MsfxBusinessStatistics> syncBusinessStatisticsDataList(){
        return msfxBusinessStatisticsMapper.syncBusinessStatisticsDataList();
    }

}
